package cn.tedu.sp06;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@EnableCircuitBreaker//Hystrix 启用断路器的注解
public class Sp06RibbonApplication {

	public static void main(String[] args) {
		SpringApplication.run(Sp06RibbonApplication.class, args);
	}

	/**
	 * 创建RestTemplate实例,放入Spring容器
	 * @return RestTemplate实例
	 */
	@Bean
	@LoadBalanced //对RestTemplate进行封装,增强RestTemplate的功能
	public RestTemplate restTemplate(){
		SimpleClientHttpRequestFactory f = new SimpleClientHttpRequestFactory();
		//注意两个超时参数默认是-1,表示不启用,只要不响应就一直等待
		f.setConnectTimeout(1000);//设置为1s则超过了一秒就认为失败了,要重试
		f.setReadTimeout(1000);
		return new RestTemplate(f);
	}
}
